package com.yeyks.task;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.yeyks.common.constants.MessageTemplate;
import com.yeyks.common.constants.consumer.UserInviteRelationTypeConstant;
import com.yeyks.common.em.MessageNoticeType;
import com.yeyks.common.em.UserBillType;
import com.yeyks.common.utils.FormatUtils;
import com.yeyks.commonReference.service.consumer.MessageNoticeTemplateService;
import com.yeyks.consumer.param.UserAccountOverview.UserAccountOverviewMakerLeaderboardListInnerVo;
import com.yeyks.consumer.param.UserAccountOverview.UserAccountOverviewMakerLeaderboardListVo;
import com.yeyks.consumer.service.MessageNoticeService;
import com.yeyks.consumer.service.UserAccountOverviewService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
@Slf4j
public class UserMakerTask {

    @Autowired
    private UserAccountOverviewService userAccountOverviewService;
    @Autowired
    private MessageNoticeTemplateService messageNoticeTemplateService;
    @Autowired
    private MessageNoticeService messageNoticeService;


    @Scheduled(cron = "0 0 0 1 * ?")
    public void fajiangjinScheduled() {
        String monthStr = DateUtil.format(DateUtil.offsetMonth(new Date(), -1), "yyyyMM");
        UserAccountOverviewMakerLeaderboardListVo userAccountOverviewMakerLeaderboardListVo = userAccountOverviewService.makerLeaderboardList(monthStr);
        if (Objects.nonNull(userAccountOverviewMakerLeaderboardListVo)) {
            fajiangjinheh(userAccountOverviewMakerLeaderboardListVo.getOne(), 60000);
            fajiangjinheh(userAccountOverviewMakerLeaderboardListVo.getTwo(), 50000);
            fajiangjinheh(userAccountOverviewMakerLeaderboardListVo.getThree(), 40000);
            List<UserAccountOverviewMakerLeaderboardListInnerVo> list = userAccountOverviewMakerLeaderboardListVo.getList();
            if (CollectionUtil.isNotEmpty(list)) {
                int i = 0;
                int amount = 30000;
                for (UserAccountOverviewMakerLeaderboardListInnerVo userAccountOverviewMakerLeaderboardListInnerVo : list) {
                    fajiangjinheh(userAccountOverviewMakerLeaderboardListInnerVo, amount);
                    i++;
                    amount -= 10000;
                    if (i >= 2) {
                        break;
                    }
                }
            }
        }
    }

    //给个人发奖金
    private void fajiangjinheh(UserAccountOverviewMakerLeaderboardListInnerVo one, Integer amount) {
        if (Objects.nonNull(one)) {
            fajiangjin(amount, one.getUserId());
        }
    }

    //每月发奖金
    private void fajiangjin(Integer amount, Integer userId) {
        Boolean isSuccess = userAccountOverviewService.addAmount(amount, userId, UserBillType.COMMSION.value(), UserBillType.COMMSION.desc(), UserInviteRelationTypeConstant.INVITE_PAY);
        if (!isSuccess) {
            log.info("佣金增加余额失败userId = {}, amount = {}", userId, amount);
        }
        try {
            Map<String, Object> param = new HashMap<>();
            param.put("amount", FormatUtils.fenToYuan(amount));
            Map<String, String> map = messageNoticeTemplateService.commonValue(param, MessageTemplate.USER_MAKER_AMOUNT_PRIZE);
            int res = messageNoticeService.activieyCreateAndSave(map, Integer.valueOf(userId), MessageNoticeType.activity_commission.value());
            if (res != 1) {
                log.info("写入佣金消息异常");
            }
        } catch (Exception e) {
            log.error("写入佣金消息异常{}", e.getLocalizedMessage());
            e.printStackTrace();
        }
    }

}
